/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>商品订单</h3>
        <h5>商城实物商品交易订单查询及管理</h5>
      </div>
    </div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>点击查看操作将显示订单（包括订单物品）的详细信息</li>
          <li>如果平台已确认收到买家的付款，但系统支付状态并未变更，可以点击收到货款操作(仅限于下单后7日内可更改收款状态)，并填写相关信息后更改订单支付状态</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>

    <div class="mDiv">
      <div class="ftitle">
        <h3>订单列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <div class="fbutton" @click="orderExport()">
        <div title="选定行数据导出excel文件,如果不选中行，将导出列表所有数据" class="add">
          <span>
            <i class="fa fa-share"></i>导出订单
          </span>
        </div>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <div class="searchbox">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="起始时间"
          end-placeholder="截止时间"
          size="small"
        ></el-date-picker>
        <el-select v-model="payStatus" size="small" clearable placeholder="支付状态">
          <el-option
            v-for="item in payStatusArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-select v-model="payCode" size="small" clearable placeholder="支付方式">
          <el-option
            v-for="item in payCodeArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-select v-model="shippingStatus" size="small" clearable placeholder="发货状态">
          <el-option
            v-for="item in shippingStatusArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-select v-model="orderStatus" size="small" clearable placeholder="订单状态">
          <el-option
            v-for="item in orderStatusArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-select v-model="columnSearch" size="small" clearable placeholder="相关搜索">
          <el-option
            v-for="item in columnSearchArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-input
          class="searchinput"
          v-model="keywords"
          size="small"
          placeholder="搜索相关数据"
          clearable
        ></el-input>
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getPage()"
        >搜索</el-button>
      </div>
    </div>
    <div class="tablebox">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="订单编号" prop="order_sn" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="收货人" prop="consignee_desc" width="180"></el-table-column>
        <el-table-column label="总金额" prop="goods_price" width="100"></el-table-column>
        <el-table-column label="应付金额" prop="order_amount" width="100"></el-table-column>
        <el-table-column label="订单状态" prop="order_status_desc" width="100"></el-table-column>
        <el-table-column label="支付状态" prop="pay_status_detail" width="100"></el-table-column>
        <el-table-column label="发货状态" prop="shipping_status_detail" width="100"></el-table-column>
        <el-table-column label="支付方式" prop="pay_name" width="120"></el-table-column>
        <el-table-column label="配送方式" prop="shipping_name" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="下单时间" prop="add_time_detail" width="180"></el-table-column>
        <el-table-column label="店铺" prop="store_name" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                size="small"
                @click="orderDetail(scope.row.order_sn)"
                icon="el-icon-s-order"
              >查看</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getOrderPage } from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
      dateRange: [
        new Date(new Date().toLocaleDateString()).getTime() -
          60 * 60 * 24 * 1000 * 90,
        new Date(new Date().toLocaleDateString()).getTime() +
          (60 * 60 * 24 * 1000 - 1000)
      ],
      payStatus: null,
      payCode: null,
      shippingStatus: null,
      orderStatus: null,
      columnSearch: null,
      keywords: null,
      payStatusArr: [
        {
          value: 0,
          label: "未支付"
        },
        {
          value: 1,
          label: "已支付"
        }
      ],
      payCodeArr: [
        {
          value: "unionpay",
          label: "银联在线支付"
        },
        {
          value: "tenpay",
          label: "PC端财付通"
        },
        {
          value: "alipay",
          label: "支付宝支付"
        },
        {
          value: "weixin",
          label: "微信支付"
        },
        {
          value: "miniAppPay",
          label: "微信小程序支付"
        }
      ],
      shippingStatusArr: [
        {
          value: 0,
          label: "未发货"
        },
        {
          value: 1,
          label: "已发货"
        },
        {
          value: 2,
          label: "部分发货"
        }
      ],
      orderStatusArr: [
        {
          value: 0,
          label: "待确认"
        },
        {
          value: 1,
          label: "已确认"
        },
        {
          value: 2,
          label: "已收货"
        },
        {
          value: 3,
          label: "已取消"
        },
        {
          value: 4,
          label: "已评价"
        },
        {
          value: 5,
          label: "已关闭"
        }
      ],
      columnSearchArr: [
        {
          value: "store_name",
          label: "店铺名称"
        },
        {
          value: "consignee",
          label: "收货人"
        },
        {
          value: "order_sn",
          label: "订单编号"
        }
      ]
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      var params = {
        user_id: this.$route.params.user_id,
        pay_status: this.payStatus,
        shipping_status: this.shippingStatus,
        pay_code: this.payCode,
        order_status: this.orderStatus,
        p: this.currentPage,
        size: this.pageSize
      };
      if (this.dateRange) {
        params.add_time_start = this.dateRange[0] / 1000;
        params.add_time_end = this.dateRange[1] / 1000 + 86400;
      }
      if (this.columnSearch == "store_name" && this.keywords) {
        params.store_name = this.keywords;
      } else if (this.columnSearch == "consignee" && this.keywords) {
        params.consignee = this.keywords;
      } else if (this.columnSearch == "order_sn" && this.keywords) {
        params.order_sn = this.keywords;
      }
      getOrderPage(params).then(function(res) {
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    refresh() {
      this.getPage();
      this.$message.success("刷新成功！");
    },
    orderDetail(orderSn) {
      this.$router.push({
        name: "order_detail",
        params: {
          sn: orderSn,
          currentPage: this.currentPage
        }
      });
    },
    orderExport() {
      //获取选中的记录
      var rows = this.$refs.multipleTable.selection;
      var ids = [];
      rows.forEach(element => {
        ids.push(element.order_id);
      });
      //拼接参数
      var paramsStr = "s=1";
      if (this.totalCount == 0) {
        this.$message.info("当前条件没有可导出的数据");
        return;
      } else if (ids.length > 0) {
        paramsStr += "&order_ids=" + ids.join(",");
      } else {
        if (this.$route.params.user_id) {
          paramsStr += "&user_id=" + this.$route.params.user_id;
        }
        if (this.payStatus != null && this.payStatus !== "") {
          paramsStr += "&pay_status=" + this.payStatus;
        }
        if (this.shippingStatus != null && this.shippingStatus !== "") {
          paramsStr += "&shipping_status=" + this.shippingStatus;
        }
        if (this.orderStatus != null && this.orderStatus !== "") {
          paramsStr += "&order_status=" + this.orderStatus;
        }
        if (this.payCode != null && this.payCode !== "") {
          paramsStr += "&pay_code=" + this.payCode;
        }
        if (this.dateRange) {
          paramsStr +=
            "&add_time_begin=" +
            this.dateRange[0] / 1000 +
            "&add_time_end=" +
            (this.dateRange[1] / 1000 + 86400);
        }
        if (this.keywords) {
          if (this.columnSearch == "store_name") {
            paramsStr += "&store_name=" + this.keywords;
          } else if (this.columnSearch == "consignee") {
            paramsStr += "&consignee=" + this.keywords;
          } else if (this.columnSearch == "order_sn") {
            paramsStr += "&order_sn=" + this.keywords;
          }
        }
      }
      window.location.href = this.apiHead + "/order/export?" + paramsStr;
    }
  }
};
</script>

<style scoped>
.searchinput {
  width: auto;
}
</style>
